// pages/RecordList/RecordList.js

var utilsTest = require("../../utils/transhform.js");//日期转换
const app = getApp();
// utilsTest.formatDatedddd(parseInt(res.data.data.endtime))
const net = require('../../utils/request.js');

Page({

  data: {
    arrlist:[],//签到列表
    componets: [ ],//加息券列表
    showType:1,//showtype 为1 的时候是签到记录 为2 的时候是 奖品;
    user_id:'',
    user_token:'',
    isshow:0,//不显示背景
    isshowTitle:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    // state 为1 的时候 礼物   为0 的时候记录;
    console.log(options.state);
    if (options.state == 1){
      that.setData({
        showType:2,
      });
    }else{
      that.setData({
        showType:1,
      });
    }


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    app.globalData.userInfo = wx.getStorageSync('userInfo');
    console.log(app.globalData.userInfo);
    var that = this;
    if (app.globalData.userInfo) {
      that.setData({
        user_id: app.globalData.userInfo.user_id,
        user_token: app.globalData.userInfo.user_token,
      });

      if(that.data.showType==2){
        that.giftList();
      }else{
        that.recordList();
      }
      
      
    
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */

  onShareAppMessage: function (e) {
    if (e.from === 'button') {
      return {
        title: '连续签到领7%加息券',
        imageUrl: '../../image/banner.jpg',
        path: 'pages/index/index?'
      }
    } else {
      return {
        title: '连续签到领7%加息券',
        imageUrl: '../../image/banner.jpg',
        path: 'pages/index/index?'
      }
    }
  },
  recordList:function(){
    var that = this;
    
    net.requestHttp("welfare/index/getSignInfo", { user_id: that.data.user_id, user_token: that.data.user_token }, function (res) {
      console.log(res);
      if (res.data.state == 1) {
        var listArr = res.data.data;
        var listArrNew = new Array();
        //sign_date
        for(var i=0;i<listArr.length;i++){
          var dic = listArr[i];
          var startTime = utilsTest.formatDatedddd(parseInt(dic['addtime']));
          var dicT = { 'sign_date': startTime};
          listArrNew.push(dicT);
        }

        that.setData({
          arrlist: listArrNew,
        });
      
        if (listArrNew.length>=10){
          that.setData({
            isshowTitle: 1,
          });
        }else{
          that.setData({
            isshowTitle: 0,
          });
        }

        if(listArrNew.length==0){
          if(that.data.showType==1){
            that.setData({
              isshow:1,
            });
            return;
          }
        }
        that.setData({
          isshow: 0,
        });
      }
    });
  },
  giftList:function(){
    var that = this;
    net.requestHttp("welfare/index/getReceiveInfo", { user_id: that.data.user_id, user_token: that.data.user_token }, function (res) {
      if (res.data.state == 1) {
        var comarr = res.data.data;
        var arrayObj = new Array();
        for(var i=0; i<comarr.length;i++){
          var dic = comarr[i];
          var startTime = dic["starttime"];
          var endTime = dic["endtime"];
          var trstartTime = utilsTest.formatDatedddd(parseInt(startTime));
          var trendTime = utilsTest.formatDatedddd(parseInt(endTime));
          dic["starttime"] = trstartTime;
          dic["endtime"] = trendTime;
          arrayObj.push(dic);
        }
        

        that.setData({
          componets: arrayObj,
        });

        if (arrayObj.length >= 10) {
          that.setData({
            isshowTitle: 1,
          });
        } else {
          that.setData({
            isshowTitle: 0,
          });
        }
        if (arrayObj.length==0){
          if(that.data.showType==2){
            that.setData({
              isshow: 1,
            });
            return;
          }
        }
        that.setData({
          isshow: 0,
        });

      }
    });
  },
  switch_itemrecord:function(){
    var that = this;
    that.setData({
      showType:1,
    });
    that.recordList();
  },
  switch_itemgift:function(){
    var that = this;
    that.setData({
      showType: 2,
    });
    that.giftList();
  }

  

})